#include "DGVPlotVTK.h"

#include <QFileInfo>

DGVPlotVTK::DGVPlotVTK(QWidget *parent) : QVTKWidget(parent)
{
    plotActor = vtkXYPlotActor::New();
    renderer = vtkRenderer::New();
    renderWin = vtkRenderWindow::New();
}

DGVPlotVTK::~DGVPlotVTK()
{
    plotActor->Delete();
    renderer->Delete();
    renderWin->Delete();
}

void DGVPlotVTK::setName(QString filename)
{
    name = filename;
    setWindowTitle(strippedNamePrefix());
    plotActor->SetTitle(strippedName().toStdString().c_str());
}

void DGVPlotVTK::setData(vtkImageData *data)
{
    plotActor->AddInput(data);
    loaded = true;
}

void DGVPlotVTK::setRanges(double xmin, double xmax, double ymin, double ymax)
{
    plotActor->SetXRange(xmin, xmax);
    plotActor->SetYRange(ymin, ymax);
}

void DGVPlotVTK::generatePlot()
{
    if(loaded)
    {
        double *pos = plotActor->GetPosition();
        double factor = 0.8;

        plotActor->SetLabelFormat("%g");
        plotActor->SetWidth(factor);
        plotActor->SetHeight(factor); //Scale
        plotActor->SetPosition(pos[0]-factor*pos[0], pos[1]-factor*pos[1]); //Reposition

        renderer->AddActor2D(plotActor);
        renderer->ResetCamera();
        renderer->SetBackground(0.1, 0.2, 0.4);
        renderWin->AddRenderer(renderer);
        renderWin->SetSize(6*minWindowSize/4,6*minWindowSize/4);
        QVTKWidget::SetRenderWindow(renderWin);
        QVTKWidget::resize(6*minWindowSize/4,6*minWindowSize/4);
    }
}

QString DGVPlotVTK::strippedName()
{
    return QFileInfo(name).fileName();
}

QString DGVPlotVTK::strippedNamePrefix()
{
    return "Plot: " + QFileInfo(name).fileName();
}
